
package com.ce.cases.hear.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ce.cases.hear.dao.po.CaseSourceCollegialDraw;
import com.ce.cases.hear.dao.po.CaseSourceHear;
import com.ce.cases.hear.logic.ICaseHearLogic;
import com.ce.cases.source.dao.po.CaseSource;
import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.framework.util.GeneratorUUID;
import com.ce.framework.util.StringUtil;
import com.ce.sys.login.model.User;
import com.ce.util.FlowWorkUtil;
import com.ce.workflow.work.dao.po.WorkflowFlow;


/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：　andy
* 创建日期：2014-04-08
* 创建时间：15:05:29
* 功能描述：案源审理 [CASE_SOURCE_HEAR]更改时用的Action
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class CaseSourceHearAction extends DataMaintainAction 
{
	private static final long serialVersionUID = 1L;
	
	private CaseSourceCollegialDraw drawPo;
	private ICaseHearLogic caseHearLogic;
	/**
	 * 审理情况登记页
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String hearRegPage(){
		  String caseId=this.getSelectId();
		  WorkflowFlow flowing=FlowWorkUtil.getWorkflowFlowByPersonCling(caseId, User.getUserId());//当前人正在处理的节点
			if(flowing==null||flowing.getVcSequence()!=3){
				return this.alertMsgAndExecJs("审理环节已不能进行该操作", "$('#operateWindow').window('close');");
			}
		  try {
			List<CaseSourceHear> exeList = (List<CaseSourceHear>) this.baselogic
					.searchByHql("From CaseSourceHear po where po.caseId='"+ caseId + "'");
			if(null!=exeList&&exeList.size()>0){//
				po=exeList.get(0);
				List<CaseSourceCollegialDraw> drawList = new ArrayList<CaseSourceCollegialDraw>();
				try {
					drawList = (List<CaseSourceCollegialDraw>) this.baselogic
						.searchByHql("From CaseSourceCollegialDraw po where po.vcDelFlag='0' and po.caseId='"+ caseId + "'");
				} catch (AppException e) {
					e.printStackTrace();
				}
				if(!drawList.isEmpty()){
					drawPo=drawList.get(0);
				}
				
			}else{
				CaseSource source = (CaseSource) this.baselogic.getPOByPk(
						"com.ce.cases.source.dao.po.CaseSource", caseId);
				BeanAccessUtil.copyProperty(po, "caseId", caseId);
				BeanAccessUtil.copyProperty(po, "organizerDept", source.getInspectionDept());
				BeanAccessUtil.copyProperty(po, "caseNumber", source.getCaseNumber());
				BeanAccessUtil.copyProperty(po, "enterprice", source.getInvolvedEnterprice());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openHearRegEdit";
	}
	/**
	 * 审理情况登记保存
	 * @return
	 */
	public String hearReg(){
		boolean flag=caseHearLogic.dealHearReg(po);
		CaseSourceHear hearPO=(CaseSourceHear)po;
		List<CaseSourceCollegialDraw> exeList = new ArrayList<CaseSourceCollegialDraw>();
		try {
		exeList = (List<CaseSourceCollegialDraw>) this.baselogic
				.searchByHql("From CaseSourceCollegialDraw po where po.vcDelFlag='0' and po.caseId='"+ hearPO.getCaseId() + "'");
		} catch (AppException e) {
			e.printStackTrace();
		}
		if(null!=hearPO.getIsCollegiate()&&1==(hearPO.getIsCollegiate()))
		{
			try {		
			
			if(!exeList.isEmpty()){
				CaseSourceCollegialDraw drawPoTemp=exeList.get(0);
				//drawPo=exeList.get(0);
				drawPoTemp.setOrganizerDept(drawPo.getOrganizerDept());
				drawPoTemp.setCaseName(drawPo.getCaseName());
				drawPoTemp.setCaseDate(drawPo.getCaseDate());
				drawPoTemp.setInvolvedMoney(drawPo.getInvolvedMoney());
				drawPoTemp.setCaseDescribe(drawPo.getCaseDescribe());
				drawPoTemp.setApprover(drawPo.getApprover());
				drawPoTemp.setApprovalDate(drawPo.getApprovalDate());
				this.baselogic.updatePo(drawPoTemp);
			}else{
				drawPo.setVcId(GeneratorUUID.generateUUID());
				drawPo.setRegisterPerson(User.getUserName());
				drawPo.setRegisterDeptId(User.getUserOrgId());
				drawPo.setRegisterDate(new Date());
				drawPo.setCaseId(hearPO.getCaseId());
				drawPo.setCaseNumber(hearPO.getCaseNumber());
				
				this.baselogic.addPo(drawPo);
			}
			} catch (AppException e) {
				e.printStackTrace();
			}
		}else{
			if(!exeList.isEmpty()){
				drawPo=exeList.get(0);
				drawPo.setVcDelFlag(1);
				try {
					this.baselogic.updatePo(drawPo);
				} catch (AppException e) {
					e.printStackTrace();
				}
			}
		}
//		try {
//			if (StringUtil.isNotBlank(drawPo.getVcId())) {
//				this.baselogic.updatePo(drawPo);
//			} else {
//				drawPo.setVcId(GeneratorUUID.generateUUID());
//				drawPo.setRegisterPerson(User.getUserName());
//				drawPo.setRegisterDeptId(User.getUserOrgId());
//				drawPo.setRegisterDate(new Date());
//				this.baselogic.addPo(drawPo);
//			}
//		} catch (AppException e) {
//			e.printStackTrace();
//		}
		
		if(!flag)
			return this.returnInfoForJS(false, "审理情况保存失败！");
		
		return this.returnInfoForJS(true, "审理情况保存成功！");
	}
	/**
	 * 查补情况登记页
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String fillRegPage(){
		  String caseId=this.getSelectId();
		  WorkflowFlow flowing=FlowWorkUtil.getWorkflowFlowByPersonCling(caseId, User.getUserId());//当前人正在处理的节点
			if(flowing==null||flowing.getVcSequence()!=3){
				return this.alertMsgAndExecJs("审理环节已不能进行该操作", "$('#operateWindow').window('close');");
			}
		  try {
			List<CaseSourceHear> exeList = (List<CaseSourceHear>) this.baselogic
					.searchByHql("From CaseSourceHear po where po.caseId='"+ caseId + "'");
			if(!exeList.isEmpty()){
				po=exeList.get(0);
			}else{
				CaseSource source = (CaseSource) this.baselogic.getPOByPk(
						"com.ce.cases.source.dao.po.CaseSource", caseId);
				BeanAccessUtil.copyProperty(po, "caseId", caseId);
				BeanAccessUtil.copyProperty(po, "caseNumber", source.getCaseNumber());
				BeanAccessUtil.copyProperty(po, "enterprice", source.getInvolvedEnterprice());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openFillRegEdit";
	}
	/**
	 * 查补情况登记保存
	 * @return
	 */
	public String fillReg(){
		boolean flag=caseHearLogic.dealFillReg(po);
		if(!flag)
			return this.returnInfoForJS(false, "查补情况保存失败！");
		return this.returnInfoForJS(true, "查补情况保存成功！");
	}
	/**
	 * 发票违法情况登记页
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String llegalRegPage(){
		 String caseId=this.getSelectId();
		 WorkflowFlow flowing=FlowWorkUtil.getWorkflowFlowByPersonCling(caseId, User.getUserId());//当前人正在处理的节点
			if(flowing==null||flowing.getVcSequence()!=3){
				return this.alertMsgAndExecJs("审理环节已不能进行该操作", "$('#operateWindow').window('close');");
			}
		  try {
			List<CaseSourceHear> exeList = (List<CaseSourceHear>) this.baselogic
					.searchByHql("From CaseSourceHear po where po.caseId='"+ caseId + "'");
			if(!exeList.isEmpty()){
				po=exeList.get(0);
			}else{
				CaseSource source = (CaseSource) this.baselogic.getPOByPk(
						"com.ce.cases.source.dao.po.CaseSource", caseId);
				BeanAccessUtil.copyProperty(po, "caseId", caseId);
				BeanAccessUtil.copyProperty(po, "caseNumber", source.getCaseNumber());
				BeanAccessUtil.copyProperty(po, "enterprice", source.getInvolvedEnterprice());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openIllegalRegEdit";
	}
	/**
	 * 发票违法情况登记保存
	 * @return
	 */
	public String llegalReg(){
		boolean flag=caseHearLogic.dealIlegalReg(po);
		if(!flag)
			return this.returnInfoForJS(false, "发票违法情况保存失败！");
		return this.returnInfoForJS(true, "发票违法情况保存成功！");
	}
	
	/**
	 * 合议提请页
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String drawRegPage(){
		 String caseId=this.getSelectId();
		 WorkflowFlow flowing=FlowWorkUtil.getWorkflowFlowByPersonCling(caseId, User.getUserId());//当前人正在处理的节点
			if(flowing==null||flowing.getVcSequence()!=3){
				return this.alertMsgAndExecJs("审理环节已不能进行该操作", "$('#operateWindow').window('close');");
			}
		  try {
			List<CaseSourceCollegialDraw> exeList = (List<CaseSourceCollegialDraw>) this.baselogic
					.searchByHql("From CaseSourceCollegialDraw po where po.caseId='"+ caseId + "'");
			if(!exeList.isEmpty()){
				drawPo=exeList.get(0);
			}else{
				CaseSource source = (CaseSource) this.baselogic.getPOByPk(
						"com.ce.cases.source.dao.po.CaseSource", caseId);
				BeanAccessUtil.copyProperty(drawPo, "caseId", caseId);
				BeanAccessUtil.copyProperty(drawPo, "caseNumber", source.getCaseNumber());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openDrawEdit";
	}
	/**
	 * 合议提请保存
	 * @return
	 */
	public String drawReg() {
		try {
			if (StringUtil.isNotBlank(drawPo.getVcId())) {
				this.baselogic.updatePo(drawPo);
			} else {
				drawPo.setVcId(GeneratorUUID.generateUUID());
				drawPo.setRegisterPerson(User.getUserName());
				drawPo.setRegisterDeptId(User.getUserOrgId());
				drawPo.setRegisterDate(new Date());
				this.baselogic.addPo(drawPo);
			}
		} catch (AppException e) {
			e.printStackTrace();
		}
		return this.returnInfoForJS(true, "合议提请保存成功！");
	}
	
	
	
	/**
	 * 后续跟踪页
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String trackRegPage(){
		 String caseId=this.getSelectId();
		  try {
			List<CaseSourceHear> exeList = (List<CaseSourceHear>) this.baselogic
					.searchByHql("From CaseSourceHear po where po.caseId='"+ caseId + "'");
			if(!exeList.isEmpty()){
				po=exeList.get(0);
			}else{
				CaseSource source = (CaseSource) this.baselogic.getPOByPk(
						"com.ce.cases.source.dao.po.CaseSource", caseId);
				BeanAccessUtil.copyProperty(po, "caseId", caseId);
				BeanAccessUtil.copyProperty(po, "caseNumber", source.getCaseNumber());
				BeanAccessUtil.copyProperty(po, "enterprice", source.getInvolvedEnterprice());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openTrackEdit";
	}
	/**
	 * 后续跟踪保存
	 * @return
	 */
	public String trackReg(){
		boolean flag=caseHearLogic.dealTrackReg(po);
		if(!flag)
			return this.returnInfoForJS(false, "后续跟踪保存失败！");
		return this.returnInfoForJS(true, "后续跟踪保存成功！");
	}
	
	/**
	 * 司法移送页
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String judicialRegPage(){
		 String caseId=this.getSelectId();
		  try {
			List<CaseSourceHear> exeList = (List<CaseSourceHear>) this.baselogic
					.searchByHql("From CaseSourceHear po where po.caseId='"+ caseId + "'");
			if(!exeList.isEmpty()){
				po=exeList.get(0);
			}else{
				CaseSource source = (CaseSource) this.baselogic.getPOByPk(
						"com.ce.cases.source.dao.po.CaseSource", caseId);
				BeanAccessUtil.copyProperty(po, "caseId", caseId);
				BeanAccessUtil.copyProperty(po, "caseNumber", source.getCaseNumber());
				BeanAccessUtil.copyProperty(po, "enterprice", source.getInvolvedEnterprice());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openJudicialEdit";
	}
	/**
	 * 司法移送保存
	 * @return
	 */
	public String judicialReg(){
		boolean flag=caseHearLogic.dealJudicialReg(po);
		if(!flag)
			return this.returnInfoForJS(false, "司法移送保存失败！");
		return this.returnInfoForJS(true, "司法移送保存成功！");
	}
	public CaseSourceCollegialDraw getDrawPo() {
		return drawPo;
	}
	public void setDrawPo(CaseSourceCollegialDraw drawPo) {
		this.drawPo = drawPo;
	}
	public ICaseHearLogic getCaseHearLogic() {
		return caseHearLogic;
	}
	public void setCaseHearLogic(ICaseHearLogic caseHearLogic) {
		this.caseHearLogic = caseHearLogic;
	}
	
	

}

